package com.tinem.platform.web.auth.controller;

import com.tinem.platform.client.security.VerificationCodeClient;
import com.tinem.platform.module.pojo.co.SessionKeyEnum;
import com.tinem.platform.module.pojo.vo.error.UserException;
import com.tinem.platform.module.pojo.vo.security.verification.code.GenerateReq;
import com.tinem.platform.module.pojo.vo.security.verification.code.GenerateRes;
import com.tinem.platform.module.pojo.vo.security.verification.code.VerificationReq;
import cn.hutool.core.lang.Validator;
import com.tinem.platform.module.starter.sdk.MessageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@Controller
public class IndexController {

    @Autowired
    VerificationCodeClient verificationCodeClient;

    @RequestMapping("/index.do")
    public String index(Map<String, Object> model, HttpServletRequest request) throws Exception {
        return "index";
    }

    @GetMapping("/verification/code/image.do")
    @ResponseBody
    public String imageCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
        GenerateRes generateRes = verificationCodeClient.generate("image",new GenerateReq());
        request.getSession().setAttribute(SessionKeyEnum.verification_code_image.getKey(),generateRes.getCodeId());
        return generateRes.getImageBase64();
    }

    @GetMapping("/verification/code/send.do")
    @ResponseBody
    public void send(@RequestParam String to, @RequestParam String imageCode,HttpServletRequest request, HttpServletResponse response) throws IOException {
        Long codeId = (Long) request.getSession().getAttribute(SessionKeyEnum.verification_code_image.getKey());
        if(codeId == null){
            throw new UserException(MessageCode.ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE_IMAGE);
        }
        if(!verificationCodeClient.verification(new VerificationReq(to,codeId,imageCode)).isSuccess()){
            throw new UserException(MessageCode.ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE);
        }
        String channel = null;
        if(Validator.isEmail(to)){
            channel = "email";
        }
        if(Validator.isMobile(to)){
            channel = "sms";
        }
        if(channel == null){
            throw new UserException(MessageCode.ERROR_PLATFORM_CLOUD_WEB_AUTH_CODE_TO);
        }
        GenerateReq req = new GenerateReq();
        req.setTo(to);
        req.setBody("【Tinem】您的验证码是:${{code}}，该验证码5分钟内有效，请勿泄漏于他人！");
        GenerateRes generateRes = verificationCodeClient.generate(channel,req);
        request.getSession().setAttribute(SessionKeyEnum.verification_code_send.getKey(to),generateRes.getCodeId());
    }
    @GetMapping("/error")
    @ResponseBody
    public String send(HttpServletRequest request, HttpServletResponse response) {
        return "error";
    }
}
